#include "city_node.h"
#include "city_map.h"

#include "graphics/graphics_util.h"
#include "console/console.h"
#include "font/font_manager.h"
#include "dd/dd_man.h"
#include "util/timer.h"				// log
#include "util/log.h"				// log
#include "util/common_macros.h"		// macros
#include "util/mmgr.h"		    
#include "script/scripthelper.h"
#include <luabind/luabind.hpp>

using namespace city;


CityNode::CityNode(int NodeID, CityMap* pScene) :
	m_pScene(pScene),
		m_ID(NodeID),
		m_Quality(1),
		m_Owner(0),
		m_pData(NULL)
{
	m_pData = m_pScene->GetCityData(m_ID);
	if (m_pData)
	{
		// setup images
		m_CityOver.Create(m_pData->MouseOver);
		m_CityOff.Create(m_pData->MouseOff);
	}
}

CityNode::~CityNode()
{

}

unsigned int			CityNode::GetColor()
{
	// base on owner?
	return ColorRGBA(0, 255, 0, 255);
}

float					CityNode::GetRadius()
{
	return (5.0f/800.0f)+ (GetQuality() * (5.0f/800.0f));
}


void					CityNode::SetQuality(int ID)
{
	m_Quality = ID;
	UTIL_CLAMP(m_Quality, 1, 10);
}

void	CityNode::SetOwner(int ID)
{
	m_Owner = ID;
	if (m_Owner == 0) m_Flag.Create("flag_neutral"); else
	if (m_Owner == 1) m_Flag.Create("flag_player"); else
	if (m_Owner == 2) m_Flag.Create("flag_ai1"); else
	if (m_Owner == 3) m_Flag.Create("flag_ai2"); else
	if (m_Owner == 4) m_Flag.Create("flag_ai3"); else
	if (m_Owner == 5) m_Flag.Create("flag_ai4"); 
}

bool	CityNode::IsInside(const math::Vec2& Pos)
{
	float	w = 20/800.0f;
	float	h = 20/600.0f;
	if (m_CityOver.IsValid())
	{
		w = m_CityOver.m_Size.x * 0.5f;
		h = m_CityOver.m_Size.y * 0.5f;

	}
	return (
		UTIL_RANGE(Pos.x, m_Pos.x-w, m_Pos.x+w) && 
		UTIL_RANGE(Pos.y, m_Pos.y-h, m_Pos.y+h) );
}
bool	CityNode::IsMouseOver(const math::Vec2& Offset)
{
	// cache this information instead?
	math::Vec2	Pos = m_pScene->GetFocus() + Offset;
	return IsInside(Pos);
}

void	CityNode::Render(const math::Vec2& Offset)
{
	if (!m_pData) return;
	if (UTIL_RANGE(m_Pos.x, Offset.x-0.1f, Offset.x+1.1f) && 
		UTIL_RANGE(m_Pos.y, Offset.y-0.1f, Offset.y+1.1f)) 
	{
		FontManager::Instance().PrintCentered(FontManager::Instance().GetFont("smallfont"), m_Pos-Offset+math::Vec2(0, 20/600.0f), "%s", m_pData->Name.c_str());
		if (IsMouseOver(Offset))
		{
			m_CityOver.Setup(m_Pos-Offset - math::Vec2(m_CityOver.m_Size.x*0.5f, m_CityOver.m_Size.y), m_CityOver.m_Size);
			m_CityOver.Render();
		} else
		{
			m_CityOff.Setup(m_Pos-Offset - math::Vec2(m_CityOff.m_Size.x*0.5f, m_CityOff.m_Size.y), m_CityOff.m_Size);
			m_CityOff.Render();
		}
	}
}


void	CityNode::RenderDebug(const math::Vec2& Offset)
{
	dd::Manager::Instance().AddCircle2D(m_Pos-Offset, GetRadius(), GetColor());
	if (UTIL_RANGE(m_Pos.x, Offset.x, Offset.x+1) && 
		UTIL_RANGE(m_Pos.y, Offset.y, Offset.y+1)) 
	{
//		FontManager::Instance().PrintCentered(m_Pos-Offset, "NODE_%i\n%s [%i]", m_ID, m_Type.c_str(), m_Quality);
		FontManager::Instance().PrintCentered(FontManager::Instance().GetFont("bigfont"), m_Pos-Offset, "%i", m_ID);
	}
}

void	CityNode::Tick(float dt)
{
}


bool		CityNode::HitTest(const math::Vec2& Pos)
{
	return (Pos-m_Pos).Magnitude() < GetRadius();
}

void		CityNode::OnClick()
{
	if (m_pData)
	{
		char	Buffer[256];
		sprintf(Buffer, "StartCity(%i, '%s'); ", m_ID, m_pData->Map.c_str());
		console::Console::Instance().Do(Buffer);
	}
}

bool		CityNode::TestName(const std::string& Name)
{
	if (!m_pData) return false;
	return strcmpi(m_pData->Name.c_str(), Name.c_str())==0;
}